//
//  UserdefaultsExtension.swift
//  DemoNavBar
//
//  Created by YangHuan on 2019/12/13.
//  Copyright © 2019 YangHuan. All rights reserved.
//

import Foundation
public extension UserDefaults {
    enum UserDefaultsKeys: String {
        case token
        case userInfo
        case userId
       
        case payPasswordState
        case mobile
        case nickname
        case isShowToken
        case linkInfo
        case loginTime
        
    
        static var all: [UserDefaultsKeys] {
            return [.token, .userInfo, .userId, .payPasswordState, .mobile, .isShowToken, .linkInfo, .loginTime, .nickname,]
        }
    }
    
    
    static func clearAll() {
        UserDefaultsKeys.all.forEach {
            UserDefaults.remove(forKey: $0)
        }
    }
    /// string
    static func set(value: String, forKey key: UserDefaultsKeys) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    static func string(forKey key: UserDefaultsKeys) -> String? {
        let key = key.rawValue
        return UserDefaults.standard.string(forKey: key)
    }
    /// bool
    static func set(value: Bool, forKey key: UserDefaultsKeys) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    static func bool(forKey key: UserDefaultsKeys) -> Bool {
        let key = key.rawValue
        return UserDefaults.standard.bool(forKey: key)
    }
    
    /// Int
    static func set(value: Int, forKey key: UserDefaultsKeys) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    static func int(forKey key: UserDefaultsKeys) -> Int {
        let key = key.rawValue
        return UserDefaults.standard.integer(forKey: key)
    }
    
    /// Double
    static func set(value: Double, forKey key: UserDefaultsKeys) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    static func double(forKey key: UserDefaultsKeys) -> Double {
        let key = key.rawValue
        return UserDefaults.standard.double(forKey: key)
    }
    
    static func remove(forKey key: UserDefaultsKeys) {
        let key = key.rawValue
        UserDefaults.standard.removeObject(forKey: key)
        UserDefaults.standard.synchronize()
    }
    
}

